<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    String id=request.getParameter("a");
%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:set var="ctx" value="${pageContext.request.contextPath}"></c:set>

<%--<c:choose>--%>
<%--<c:when test="${list==null}">--%>

<%--<jsp:forward page="/Book/index"></jsp:forward>--%>

<%--</c:when>--%>
<%--<c:otherwise>--%>


<html>
<script type="text/javascript" src="${ctx}/statics/js/jquery-1.8.3.min.js"></script>

<head>
    <base href="<%=basePath %>">
    <title>Title</title>
</head>
<body>
<div style="width:600px;margin: auto">
    <%--<form action="${ctx}/Book/index" method="post" >--%>


    <%--</form>--%>

    <table border="1" width="80%" id="book">
        <tr><th colspan="6"><a href="book/add" style='color:red'>帖子列表</a></th ></tr>
        <tr><th colspan="6">帖子标题：<input type="text" name="b" id="title" value="${title}"/></th></tr>
       <tr> <th colspan="6"><input type="submit" id="submit" onclick="Load()" value="搜索"/></th></tr>

        <tr style="background-color: blue" id="index">
            <th>标题</th>
            <th>内容摘要</th>
            <th>作者</th>
            <th>发布时间</th>
            <th>操作</th>
        </tr>
    </table>

    <div class="pages">

        <a href="javascript:void(0)" onclick="pages(0)">首頁</a>
        <a href="javascript:void(0)" onclick="pages(-1)">上一页</a>

        &nbsp;&nbsp;  &nbsp;&nbsp;

        <a href="javascript:void(0)" onclick="pages(1)">下一页</a>
        &nbsp;&nbsp;
        <a href="javascript:void(0)" onclick="pages(2)">末頁</a>

        &nbsp;&nbsp;  &nbsp;&nbsp;
        <span id="page">第page.currPageNo页/共page.totalPageCount页</span>
    </div>

</div>
<div style="margin: auto">
    <c:if test="${result>0}">
        <span style="color: red">添加成功</span>
    </c:if>
    <c:if test="${result<=0}">
        <span style="color: red">添加失败</span>
    </c:if>
</div>
<script src="statics\js\jquery-1.8.3.min.js"></script>
<script type="text/javascript">
    var pageIndex=1;
    var totalPageCount=0;
    $(function () {
        Load();

    })
    //查詢
    function Load() {

        $.ajax({

            url:"Invitation/all",
            type:"POST",
            data:{b:$("#title").val(),pageIndex:pageIndex},
            dataType:"json",
            success:function (data) {
                $("#index").nextAll().remove();
                totalPageCount=data.page.totalPageCount;

                var $html;
                $("#page").html("第"+data.page.currPageNo+"页/共"+totalPageCount+"页");
window.alert("12313123");
                for(var i=0;i<data.list.length;i++){

                    $html="<tr><td>"+data.list[i].title+"</td>"
                        +"<td>"+data.list[i].summary+"</td>"
                        +"<td>"+data.list[i].author+"</td>"
                        +"<td>"+data.list[i].createdate+"</td>"
                        +"<td>"+data.list[i].varchar+"</td>"
                        +"<td><a href='book/selById/"+data.list[i].bookId+"'>修改</a>"
                        +"<a href='' id='"+data.list[i].bookId+"' onclick='deleteId(this,"+data.list[i].bookId+")'"+">删除</a></td></tr>";
                    $("#book").append($html);
                }

            }


        })
    }


    function pages(CurrentPageNo){
        if(CurrentPageNo==2){
            pageIndex=totalPageCount;
        }
        else if(CurrentPageNo==0){
            pageIndex=1;
        }
        else if(CurrentPageNo==-1){
            pageIndex=pageIndex-1;
        }else if(pageIndex<1){
            pageIndex=1;
        }else if(pageIndex>totalPageCount){
            pageIndex=totalPageCount;
        }
        else{
            pageIndex=pageIndex+1;
        }
        if(pageIndex>=1&&pageIndex<=totalPageCount){
            Load();
        }
    };




    //刪除
    function deleteId (m,n) {
        if(window.confirm("确定删除么？")){

            $.ajax({
                type:"POST",
                url:"${ctx}/Book/delById",
                data:{bookId:n},
                async:false,
                dataType:"json",
                success:function (data) {
                    if(data=="1") {
                        alert("删除成功");

                    }else {
                        alert("删除失败");
                    }
                },error: function(){
                    alert(arguments[1]);
                }


            })


        }

    }

</script>
</body>
</html>
<%--</c:otherwise>--%>
<%--</c:choose>--%>